//
//  AppCommentView.swift
//  ezyform
//
//  Created by MoennigQ on 15/6/21.
//  Copyright © 2021 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class AppCommentView: View {
    let submintBtn = UIButton(type: UIButton.ButtonType.custom)
    var submitClosure: ((_ model: AppCommentModel)->Void)?
    let star1Btn = UIButton()
    let star5Btn = UIButton()
    let star4Btn = UIButton()
    let star3Btn = UIButton()
    let star2Btn = UIButton()
    let commentBtn = UIButton()
    let commentTextView = BaseTextView()
    var commentTextViewHeightContraint: Constraint?
    //for code
    var rateStar: Int = 1
    
    override func setUpView() {
        self.backgroundColor = UIColor.black.withAlphaComponent(0.8)
        //
        //back view
        let backView = UIView()
        backView.backgroundColor = UIColor.white
        self.addSubview(backView)
        backView.layer.cornerRadius = Constant.BackViewCornerRadius
        backView.snp.makeConstraints { (make) in
            make.width.equalTo(300)
            make.height.greaterThanOrEqualTo(0)
            make.center.equalToSuperview()
        }
        //title label
        let titleLabel = ContentLabel(R.font.interBold(size: 17))
        titleLabel.text = R.string.localizable.tapAStarToRateTheGreenDealApp()
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 0
        backView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.left.equalTo(24)
            make.right.equalTo(-24)
            make.height.greaterThanOrEqualTo(0)
        }
        //star 3
        backView.addSubview(star3Btn)
        star3Btn.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.height.width.equalTo(48)
            make.centerX.equalToSuperview()
        }
        //star 2
        backView.addSubview(star2Btn)
        star2Btn.snp.makeConstraints { make in
            make.right.equalTo(star3Btn.snp.left).offset(-4)
            make.height.width.centerY.equalTo(star3Btn)
        }
        //star 4
        backView.addSubview(star4Btn)
        star4Btn.snp.makeConstraints { make in
            make.left.equalTo(star3Btn.snp.right).offset(4)
            make.height.width.centerY.equalTo(star3Btn)
        }
        //star 1
        backView.addSubview(star1Btn)
        star1Btn.snp.makeConstraints { make in
            make.right.equalTo(star2Btn.snp.left).offset(-4)
            make.height.width.centerY.equalTo(star3Btn)
        }
        //star 5
        backView.addSubview(star5Btn)
        star5Btn.snp.makeConstraints { make in
            make.left.equalTo(star4Btn.snp.right).offset(4)
            make.height.width.centerY.equalTo(star3Btn)
        }
        //commint btn
        let attrString = NSMutableAttributedString(string: "Comment")
        let attr: [NSAttributedString.Key : Any] = [.font: R.font.interBold(size: 15 * Constant.currentSizeScale)!,.foregroundColor: UIColor(hexStr: "#005D2D"),.underlineStyle: NSUnderlineStyle.single.rawValue, .underlineColor: UIColor(hexStr: "#005D2D")]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        commentBtn.setAttributedTitle(attrString, for: UIControl.State.normal)
        backView.addSubview(commentBtn)
        commentBtn.snp.makeConstraints { make in
            make.top.equalTo(star3Btn.snp.bottom).offset(6)
            make.height.equalTo(40)
            make.width.equalTo(200)
            make.centerX.equalToSuperview()
        }
        //comment textview
        commentTextView.backgroundColor = .white
        commentTextView.textColor = .black
        commentTextView.setFont(R.font.sfProTextRegular(size: 14 ))
        commentTextView.isHidden = true
        commentTextView.layer.borderWidth = 1
        commentTextView.layer.borderColor = UIColor(red: 0.9, green: 0.91, blue: 0.93, alpha: 1).cgColor
        commentTextView.layer.cornerRadius = Constant.StandardCornerRadius
        commentTextView.clipsToBounds = true
        commentTextView.placeholder = "Comment(Optional)"
        backView.addSubview(commentTextView)
        commentTextView.snp.makeConstraints { make in
            make.top.equalTo(commentBtn)
            make.left.equalToSuperview().offset(26)
            make.right.equalToSuperview().offset(-26)
            self.commentTextViewHeightContraint = make.height.equalTo(20).constraint
        }
        //horizonal line view
        let lineView = View(hexStr: "#0000001A")
        backView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(commentTextView.snp.bottom).offset(25)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        //vertial line view
        let lineView1 = View(hexStr: "#0000001A")
        backView.addSubview(lineView1)
        lineView1.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(3)
            make.height.equalTo(48)
            make.width.equalTo(1)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-4)
        }
        //not now btn
        let notNowBtn = UIButton(type: UIButton.ButtonType.custom)
        let attrStringNo = NSMutableAttributedString(string: "Not now")
        let attrNo: [NSAttributedString.Key : Any] = [.font: R.font.interSemiBold(size: 17 * Constant.currentSizeScale)!,.foregroundColor: UIColor(hexStr: "#379657")]
        attrStringNo.addAttributes(attrNo, range: NSRange(location: 0, length: attrStringNo.length))
        notNowBtn.setAttributedTitle(attrStringNo , for: UIControl.State.normal)
        backView.addSubview(notNowBtn)
        notNowBtn.snp.makeConstraints { (make) in
            make.top.equalTo(lineView)
            make.left.equalToSuperview()
            make.right.equalTo(lineView1)
            make.bottom.equalToSuperview()
        }
        
        //submint button
        backView.addSubview(submintBtn)
        submintBtn.snp.makeConstraints { (make) in
            make.top.equalTo(lineView)
            make.left.equalTo(lineView1)
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        //
        star1Btn.setImage(UIImage(named: "installation_detail_app_comment_off"), for: UIControl.State.normal)
        star2Btn.setImage(UIImage(named: "installation_detail_app_comment_off"), for: UIControl.State.normal)
        star3Btn.setImage(UIImage(named: "installation_detail_app_comment_off"), for: UIControl.State.normal)
        star4Btn.setImage(UIImage(named: "installation_detail_app_comment_off"), for: UIControl.State.normal)
        star5Btn.setImage(UIImage(named: "installation_detail_app_comment_off"), for: UIControl.State.normal)
        //
        star1Btn.addTarget(self, action: #selector(star1BtnClick(_:)), for: UIControl.Event.touchUpInside)
        
        star2Btn.addTarget(self, action: #selector(star2BtnClick(_:)), for: UIControl.Event.touchUpInside)
        
        star3Btn.addTarget(self, action: #selector(star3BtnClick(_:)), for: UIControl.Event.touchUpInside)
        
        star4Btn.addTarget(self, action: #selector(star4BtnClick(_:)), for: UIControl.Event.touchUpInside)
        
        star5Btn.addTarget(self, action: #selector(star5BtnClick(_:)), for: UIControl.Event.touchUpInside)
        commentBtn.tag = 0
        commentBtn.addTarget(self, action: #selector(commentBtnClick(_:)), for: UIControl.Event.touchUpInside)
        submintBtn.addTarget(self, action: #selector(submintBtnClick), for: UIControl.Event.touchUpInside)
        notNowBtn.addTarget(self, action: #selector(notNowBtnClick), for: UIControl.Event.touchUpInside)
        //
        commentTextView.returnKeyType = .default
        self.setSubmitBtn(false)
        
    }
    fileprivate func setSubmitBtn(_ isEnabled: Bool){
        let attrStringOK = NSMutableAttributedString(string: "Submit")
        var textColor = UIColor(hexStr: "#D9D9D9")
        submintBtn.isEnabled = false
        if isEnabled {
            textColor = UIColor(hexStr: "#379657")
            submintBtn.isEnabled = true
        }
        let attrOK: [NSAttributedString.Key : Any] = [.font: R.font.interSemiBold(size: 17 * Constant.currentSizeScale)!,.foregroundColor: textColor]
        attrStringOK.addAttributes(attrOK, range: NSRange(location: 0, length: attrStringOK.length))
        
        submintBtn.setAttributedTitle(attrStringOK , for: UIControl.State.normal)
    }
    fileprivate func setAllStarBtn(){
        //1星
        if  self.rateStar >= 1 {
            star1Btn.setImage(UIImage(named: "installation_detail_app_comment_on"), for: UIControl.State.normal)
        }else{
            star1Btn.setImage(UIImage(named: "installation_detail_app_comment_off"), for: UIControl.State.normal)
        }
        //2星
        if  self.rateStar >= 2 {
            star2Btn.setImage(UIImage(named: "installation_detail_app_comment_on"), for: UIControl.State.normal)
        }else{
            star2Btn.setImage(UIImage(named: "installation_detail_app_comment_off"), for: UIControl.State.normal)
        }
        //3星
        if  self.rateStar >= 3 {
            star3Btn.setImage(UIImage(named: "installation_detail_app_comment_on"), for: UIControl.State.normal)
        }else{
            star3Btn.setImage(UIImage(named: "installation_detail_app_comment_off"), for: UIControl.State.normal)
        }
        //4星
        if  self.rateStar >= 4 {
            star4Btn.setImage(UIImage(named: "installation_detail_app_comment_on"), for: UIControl.State.normal)
        }else{
            star4Btn.setImage(UIImage(named: "installation_detail_app_comment_off"), for: UIControl.State.normal)
        }
        //5星
        if  self.rateStar >= 5 {
            star5Btn.setImage(UIImage(named: "installation_detail_app_comment_on"), for: UIControl.State.normal)
        }else{
            star5Btn.setImage(UIImage(named: "installation_detail_app_comment_off"), for: UIControl.State.normal)
        }
        if self.rateStar > 0{
            self.setSubmitBtn(true)
        }
    }
    @objc func star1BtnClick(_ sender: UIButton){
        self.rateStar = 1
        self.setAllStarBtn()
    }
    @objc func star2BtnClick(_ sender: UIButton){
        self.rateStar = 2
        self.setAllStarBtn()
    }
    @objc func star3BtnClick(_ sender: UIButton){
        self.rateStar = 3
        self.setAllStarBtn()
    }
    @objc func star4BtnClick(_ sender: UIButton){
        self.rateStar = 4
        self.setAllStarBtn()
    }
    @objc func star5BtnClick(_ sender: UIButton){
        self.rateStar = 5
        self.setAllStarBtn()
    }
    @objc func commentBtnClick(_ sender: UIButton){
        if sender.tag == 0{
            sender.tag = 1
            self.commentBtn.isHidden = true
            self.commentTextView.isHidden = false
            self.commentTextViewHeightContraint?.update(offset: 100)
        }
    }
    @objc func notNowBtnClick(){
        let model = AppCommentModel()
        model.ignoreFlag = true
        self.submitClosure?(model)
        self.removeFromSuperview()
    }
    @objc func submintBtnClick(){
        let model = AppCommentModel()
        model.rating = self.rateStar
        model.comment = (self.commentTextView.text ?? "")
        self.submitClosure?(model)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.endEditing(true)
    }
}
